package euler.p001_050;

import euler.MainEuler;

public class Euler039 extends MainEuler {
    /*
        If p is the perimeter of a right angle triangle with
        integral length sides, {a,b,c},
        there are exactly three solutions for p = 120.

        {20,48,52}, {24,45,51}, {30,40,50}

        For which value of p ≤ 1000,
        is the number of solutions maximised?

     */

    /*
        If both a and b are even, c will also be even and P (the perimeter) will be even.
        If both a and b are odd, c will be even and P will be even.
        If one is even and the other is odd, c will be odd and P will again be even.
        Therefore, only even values of P need to be checked.
     */
    public String resolve() {
        int limite = 1000;

        int maxSolutions = 0;
        int maxP = 0;

        for (int p = 12; p <= limite; p+=2) {

            int solutions = 0;

            for (int a = 3; 3*a < p; a++) {
                for (int b = Math.max(p/2 - a, a) + 1; 2*b < p - a; b++) {
                    int c = p - a - b;
                    if (c*c == a*a + b*b) {
                        solutions++;
                    }
                }
            }

            if (solutions > maxSolutions) {
                maxSolutions = solutions;
                maxP = p;
            }
        }

        return String.valueOf(maxP);
        // 840
    }

}
